package com.zkingsoft.wechat;

import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.apache.commons.fileupload.FileUploadException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.matrix.core.pojo.AjaxResult;
import com.matrix.core.tools.FileType;
import com.matrix.core.tools.LogUtil;
import com.matrix.core.web.BaseAction;
import com.zkingsoft.common.bean.SysUsers;
import com.zkingsoft.common.tools.UploadUtil;
import com.zkingsoft.laqeelin.bean.BizUser;
import com.zkingsoft.redis.RedisUserLoginUtils;

/**
 * This field was generated by Zking.software.Codegen.
 * 
 * @date 2016-11-28 15:14
 */
@Controller
@RequestMapping(value = "/wxapi/uploadFile")
public class WxFileUploadAction extends BaseAction {
	/**
	 * 文件夹类型
	 */
	String folderType = "image";

	public final static List<FileType> extList = new ArrayList<FileType>();
	static {
		extList.add(FileType.JPEG);
		extList.add(FileType.PNG);
	}
	@Autowired
	RedisUserLoginUtils redisUserLoginUtils;
	/**
	 * 通用文件上传
	 * 
	 * @author JIANGYOUYAO
	 * @email 935090232@qq.com
	 * @date 2018年7月22日
	 * @param response
	 * @param request
	 * @return
	 * @throws IOException
	 * @throws FileUploadException
	 * @throws NoSuchAlgorithmException
	 */
	@RequestMapping(value = "/doUpload")
	public void doImageUpload(HttpServletResponse response, MultipartHttpServletRequest request)
			throws IOException, FileUploadException, NoSuchAlgorithmException {
		String userId="";
		SysUsers user = getSessionUser();
		if(user==null){
			//获取登录用户
			BizUser loginUser = redisUserLoginUtils.getLoginUser();
			userId=loginUser.getUserId()+"";
		}else{
			userId=user.getSuId();
		}
		
		
		
		Map<String, String> fileMap = UploadUtil.doUpload(request, extList, folderType, userId);
		AjaxResult result = new AjaxResult();
		result.setStatus(fileMap.get("status"));
		result.putInMap("url", fileMap.get("visitPath"));
		result.putInMap("msg", fileMap.get("msg"));
		LogUtil.debug("上传结果：{}", fileMap.get("status")+"--"+fileMap.get("visitPath"));
		JSONObject json = JSONObject.fromObject(result);
		 response.getWriter().write(json.toString());
	        response.getWriter().flush();
	        response.getWriter().close();
		
	}
	
}